﻿package Welsy4.Connect.commands
{
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import flash.net.URLVariables;
	
	
	import mx.rpc.IResponder;
	import mx.controls.Alert;
	
	import Welsy4.Connect.model.ConnectModel;
	import Welsy4.Connect.events.ChainEvent;
	import Welsy4.Connect.events.ResponseEvent;
	import Welsy4.Connect.business.CommonInfoDelegate;
	import Welsy4.Connect.vo.HttpServiceHandler;
	import Welsy4.Connect.vo.SCOShortcut;
	import Welsy4.Connect.vo.Meeting;
	
	
	public class GetMeetingSCOidCommand extends SequenceCommand implements Command, IResponder
	{
		
		private var eventURLVariable:URLVariables;
		
		override public function execute(event:CairngormEvent):void {
			
			ConnectModel.getInstance().addQueue("GetMeetingSCOidCommand.execute: "+ConnectModel.getInstance().SCOShortcutRequested.sco_id);
			
			this.nextEvent = ChainEvent(event).nextChainedEvent;
			this.eventURLVariable = ChainEvent(event).eventRequest;
			
			var SCOshortcutDelegate:CommonInfoDelegate = new CommonInfoDelegate(this);
			
			// SCOShortcutRequested.sco_id è l'id della meeting folder
			SCOshortcutDelegate.getSCOContents(ConnectModel.getInstance().SCOShortcutRequested.sco_id,"meeting", eventURLVariable.name);
		}
		
		public function result(data:Object):void {
			
			ConnectModel.getInstance().addQueue("GetMeetingSCOidCommand.result");
			
			ConnectModel.getInstance().httpServiceHandler = new HttpServiceHandler();
			ConnectModel.getInstance().httpServiceHandler.setXML(data.result);
			
			if (ConnectModel.getInstance().httpServiceHandler.getResultStatus() == ConnectModel.SESSION_OK) {
				ConnectModel.getInstance().lastMeetingFound = new Meeting;
				
				if (ConnectModel.getInstance().httpServiceHandler.xml.scos.sco.length() > 0) {
					
					ConnectModel.getInstance().lastMeetingFound.sco_id = ConnectModel.getInstance().httpServiceHandler.xml.scos.sco.attribute("sco-id");
					ConnectModel.getInstance().lastMeetingFound.name = ConnectModel.getInstance().httpServiceHandler.xml.scos.sco.name;
					
					
					
					//Alert.show("meeting found: " + ConnectModel.getInstance().lastMeetingFound.sco_id );
					
					this.executeNextCommand();
					
				} else {
					CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent (ResponseEvent.ERROR, false, ResponseEvent.NO_DATA, data.result));
				}
				
				//this.executeNextCommand();
			} else {
				
			    CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent (ResponseEvent.ERROR, false, ResponseEvent.GENERIC, data.result));	
			}
			
			
			
			
		}
		
		public function fault(data:Object):void {
		    ConnectModel.getInstance().addQueue("GetMeetingSCOidCommand.fault");
			CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent (ResponseEvent.ERROR, false, ResponseEvent.CONNECTION));
		}
		
	}
	
}